﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyQuanCafe.BusinessLogic;
using QuanLyQuanCafe.BusinessObject;

namespace QuanLyQuanCafe
{
    public partial class FrmBan : Form
    {        
        private KhuVucBL khuvucBL = new KhuVucBL();
        private BanBL banBL = new BanBL();
        public FrmBan()
        {
            InitializeComponent();
            cbKV.DataSource = khuvucBL.DSKVBanHang();
            cbKV.DisplayMember = "TenKV";
            cbKV.ValueMember = "MaKV";

        }

        private void FrmBan_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = banBL.DSBanKV();
            AnHienButton(false);
           
        }

        private void AnHienButton(bool flag)
        {
            btnThem.Enabled = !flag;
            btnSua.Enabled = !flag;
            btnXoa.Enabled = !flag;
            btnLuu.Enabled = flag;
            btnHuy.Enabled = flag;

            tbMaBan.Enabled = flag;
            tbVitri.Enabled = flag;
            cbKV.Enabled = flag;
            dataGridView1.Enabled = !flag;
        }

        private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int row = e.RowIndex;
            try
            {
                tbMaBan.Text = dataGridView1.Rows[row].Cells["MaBan"].Value.ToString();
                tbVitri.Text = dataGridView1.Rows[row].Cells["ViTriBan"].Value.ToString();
                cbKV.Text = dataGridView1.Rows[row].Cells["TenKV"].Value.ToString();
            }
            catch
            {
                
            }
        }

        private void CapNhatText()
        {
            tbMaBan.Text = "";
            tbVitri.Text = "";
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            AnHienButton(true);
            CapNhatText();
            tbMaBan.Text = "";
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            AnHienButton(true);
            tbMaBan.Enabled = false;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DataTable datatemp = banBL.LayDanhSachBanTheoMaBan(tbMaBan.Text);
            if (int.Parse(datatemp.Rows[0]["TinhTrang"].ToString()) == 1)
            {
                if (MessageBox.Show("Bạn Có Muốn Xóa Bàn [" + tbVitri.Text + "] Ở " + cbKV.Text + " Không?", "Quản Lý Quán Cafe", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    if (banBL.XoaBan(tbMaBan.Text))
                        FrmBan_Load(sender, e);
            }
            else
            {
                MessageBox.Show("Bàn Đang Có Khách. Không Thể Xóa Bàn Này.", "Quán Lý Quán Cafe", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            
        }

        private BanBO LayThongTinBan()
        {
            BanBO banBO = new BanBO();
            banBO.MaBan = int.Parse(tbMaBan.Text);
            banBO.MaKV = int.Parse(cbKV.SelectedValue.ToString());
            banBO.ViTriBan = int.Parse(tbVitri.Text);

            return banBO;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                BanBO banBO = LayThongTinBan();
                if (banBL.CheckBan(banBO.MaBan))
                {
                    if (banBL.SuaBan(banBO))
                        FrmBan_Load(sender, e);
                }
                else
                {
                    if (banBL.ThemBan(banBO))
                        FrmBan_Load(sender, e);
                }
            }
            catch
            {
                MessageBox.Show("Kiểm Lại Các Thông Tin Đã Nhập.", "Quán Lý Quán Cafe", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            AnHienButton(false);
        }             
    }
}
